package matchstiklib;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class MatchstikUtil {

	public static Fractal openFractal(File f) throws ParserConfigurationException, SAXException, IOException
	{
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document doc = builder.parse(f);
		
		Fractal fractal = new Fractal();
		
		NodeList flames = doc.getElementsByTagName("flame");
		for (int i = 0; i < flames.getLength(); i++) {
			Node node = flames.item(i);
			NamedNodeMap flameattribs = node.getAttributes();
			
			String[] sizes = flameattribs.getNamedItem("size").getNodeValue().split(" ");
			fractal.width = Integer.parseInt(sizes[0]);
			fractal.height = Integer.parseInt(sizes[1]);
			
			fractal.xrange = Double.parseDouble(flameattribs.getNamedItem("xrange").getNodeValue());
			fractal.yrange = Double.parseDouble(flameattribs.getNamedItem("yrange").getNodeValue());
			
			NodeList xforms = node.getChildNodes();
			for (int j = 0; j < xforms.getLength(); j++) {
				Node xform = xforms.item(j);
				NamedNodeMap xformattribs = xform.getAttributes();
				
				FunctionModel func = new FunctionModel();
				func.weight = Double.parseDouble(xformattribs.getNamedItem("weight").getNodeValue());
				
				String[] coeffs = xformattribs.getNamedItem("coeffs").getNodeValue().split(" ");
				assert(coeffs.length >= 6);
				func.a = Double.parseDouble(coeffs[0]);
				func.b = Double.parseDouble(coeffs[1]);
				func.c = Double.parseDouble(coeffs[2]);
				func.d = Double.parseDouble(coeffs[3]);
				func.e = Double.parseDouble(coeffs[4]);
				func.f = Double.parseDouble(coeffs[5]);
				
				func.name = xformattribs.getNamedItem("name").getNodeValue();
				
				func.color = Float.parseFloat(xformattribs.getNamedItem("color").getNodeValue());
				
				fractal.funcs.add(func);
			}
		}
		
		return fractal;	
	}
	
	public static HashMap<String, int[]> openPalettes(File f) throws ParserConfigurationException, SAXException, IOException {
		
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document doc = builder.parse(f);
		
		HashMap<String, int[]> pals = new HashMap<String, int[]>();
		
		NodeList palroots = doc.getElementsByTagName("palettes");
		
		// should be only 1 entry
		assert(palroots.getLength() > 0);
		NodeList palettes = palroots.item(0).getChildNodes();
		
		for (int i = 0; i < palettes.getLength(); i++) {
			Node pal = palettes.item(i);
			NamedNodeMap palattribs = pal.getAttributes();
			
			String sdata = palattribs.getNamedItem("data").getNodeValue();
			
			int[] data = new int[256];
			String temp;
			for (int j = 0; j < 256; j++) {
				temp = sdata.substring(j*8, j*8+8);
                data[j] = Integer.valueOf(temp, 16);
			}
			
			String name = palattribs.getNamedItem("name").getNodeValue();
			
			pals.put(name, data);
		}
		
		return pals;
	}
}
